package org.bot.plugin.web.event;

import org.bot.api.event.DataStorage;
import org.bot.api.event.EventName;
import org.bot.api.event.Events;
import org.bot.api.flow.FlowNode;
import org.bot.plugin.web.util.SelectorType;
import org.bot.plugin.web.util.WebApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author 沧月
 * @create 0013 2023/9/13 17:51
 * @description <设置输入框值，默认优先使用设置的静态值，其次调取流程结果值>
 * <p>
 * config   {"cmd":"org.bot.plugin.web.event.WebSetValue","arg":"/html/body/div/div/div[2]/form/button",
 * "selectorType":"xpath","staticValue":"静态值","dataIndex":"0"}
 */
@Component
@EventName("设置值")
public class WebSetValue implements Events {

    @Autowired
    WebApi webApi;

    @Override
    public Object exec(FlowNode note, DataStorage data) {
        webApi.setValue(
                webApi.getWebDriver(note),
                SelectorType.getType(note.getSelectorType()),
                note.getArg(),
                note.getStaticValue() == null ? data.get(note.getDataIndex()) : note.getStaticValue()
        );
        return null;
    }
}
